<?php
class Api_Role extends Api {
	private $role;
	public function __construct()
	{
		parent::__construct();
		$this->role = RoleTable::getInstance();
	}

	public function create(array $role_info)
	{
		$default = array(
			'role_name' => '',
			'active' => 1,
			'description' => 'this is default description',
		);

		$role_info = array_merge($default, $role_info);

		$role = new Role();
		$role->setArray($role_info);
		
		if ($role->isValid())
		{
			$role->save();
			return TRUE;
		}
		else
		{
			return $role->getErrorStack()->toArray();
		}

	}

	public function read($param)
	{
		if (is_numeric($param))
		{
			$role = $this->role->findOneById($param);
		}
		else
		{
			$role = $this->role->findOneByName($param);			
		}

		return $role;
	}

	public function update(int $role_id, array $role_info)
	{
		$role = $this->read($role_id);
		$role->setArray($role_info);
		$role->save();
	}

	public function delete(int $role_id)
	{
		$role->$this->update($role_id, array(
			'active' => 0,
		));
	}

	public function get_all()
	{
		return $this->role->findAll();
	}
}